<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<!-- head 头部-->
<head th:replace="admin/layout/include::head('类型管理','/assets/module/treeTable/treeTable.css')"></head>

<body>
<!-- loading 动画-->
<div th:replace="admin/layout/include::loading"></div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="pull-left layui-form-pane search-box">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="btnExpandAll" class="layui-btn layui-btn-primary">
                            <i class="layui-icon layui-icon-shrink-right"></i>展开全部
                        </button>
                        <button id="btnFoldAll" class="layui-btn layui-btn-primary">
                            <i class="layui-icon layui-icon-spread-left"></i>折叠全部
                        </button>
                    </div>
                </div>
            </div>


            <!-- 数据表格 -->
            <table class="layui-table" id="data-table" lay-filter="data-table"></table>
        </div>
    </div>
</div>
</body>

<!-- 表格操作列 -->
<script type="text/html" id="data-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" ajax-event="del" ajax-url="/admin/blogType/" value="{{d.id}}">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="data-table-state">
    <input type="checkbox" lay-filter="updateState" ajax-url="/admin/blogType/updateState" value="{{d.id}}" lay-skin="switch" lay-text="正常|禁用"
           {{d.state==0?'checked':''}}/>
</script>
<script type="text/html" id="data-table-indexShow">
    {{#  if(d.indexShow == "1"){ }}
    展示
    {{#  } else{ }}
    隐藏
    {{#  }}}
</script>
<!-- js部分 -->
<div th:replace="admin/layout/include::js"></div>
<script>
    layui.use(['form', 'table', 'util', 'admin','treeTable'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var treeTable = layui.treeTable;
        var formUrl;  // 表单提交的action
        var insTb = treeTable.render({
                elem: '#data-table',
                tree: {
                    isPidData:true,
                    iconIndex: 2, // 是否是pid形式数据
                    idName: 'id',
                    pidName: 'parentId'
                },
                height : 'full-165',
                reqData: function(data, callback) {
                    // 在这里写ajax请求，通过callback方法回调数据
                    $.get('/admin/blogType/tree', function (res) {
                        callback(res.data);  // 参数是数组类型
                    });
                },
                cols: [
                    {type: 'checkbox'},
                    {field: 'id', width:50,title: '#'},
                    {field: 'typeName', minWidth: 150, title: '类型名称'},
                    {field: 'remark', title: '描述'},
                    {field: 'state',title:'状态',templet:'#data-table-state'},
                    {field: 'orderNo', width: 80, align: 'center', title: '排序'},
                    {templet: '#data-table-bar', width: 200, align: 'center', title: '操作'}
                ],
                text: {
                    none: '<div style="padding: 15px 0;">哎呀，一条数据都没有~</div>'
                }
            });

        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            showEditModel();
        });
        $('#btnExpandAll').click(function () {
            insTb.expandAll();  // 展开全部
        })
        $('#btnFoldAll').click(function () {
            insTb.foldAll();  // 折叠全部
        })
        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var keyword = $('#edtSearch').val();
            insTb.filterData(keyword);   // 根据关键字模糊搜索
        });
        // 工具条点击事件
        treeTable.on('tool(data-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            }
        });


        // 显示编辑弹窗
        var showEditModel = function (data) {
            admin.putTempData("t_type",data);
            admin.putTempData("formOk",false);
            top.layui.admin.open({
                type: 2,
                title: data ? '修改博客权限' : '添加博客权限',
                area: ['380px', '420px'],
                content: '/admin/blogType/save',
                end: function () {
                    admin.getTempData('formOk') && insTb.refresh();// 成功刷新表格
                }
            });

        };

    });

</script>
</html>